package com.interagriculture.service.impl;

import com.github.pagehelper.PageHelper;
import com.interagriculture.common.Constants;
import com.interagriculture.common.PageResult;
import com.interagriculture.entity.Journalism;
import com.interagriculture.entity.JournalismMain;
import com.interagriculture.entity.dto.JournalismDTO;
import com.interagriculture.entity.vo.JournalismDetailVO;
import com.interagriculture.entity.vo.JournalismVO;
import com.interagriculture.mapper.JournalismMainMapper;
import com.interagriculture.mapper.JournalismMapper;
import com.interagriculture.service.JournalismMainService;
import com.interagriculture.service.JournalismService;
import com.interagriculture.utils.StringTools;
import com.interagriculture.utils.ThreadLocalUtil;
import com.interagriculture.utils.UploadFileUtil;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Journalism)表服务实现类
 *
 * @author makejava
 * @since 2024-11-14 15:52:14
 */
@Service("journalismService")
public class JournalismServiceImpl implements JournalismService {
    @Resource
    private JournalismMapper journalismMapper;
    @Resource
    private JournalismMainMapper journalismMainMapper;
    @Resource
    private UploadFileUtil uploadFileUtil;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Journalism queryById(Integer id) {
        return this.journalismMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param journalism 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Journalism> queryByPage(Journalism journalism, PageRequest pageRequest) {
        long total = this.journalismMapper.count(journalism);
        return new PageImpl<>(this.journalismMapper.queryAllByLimit(journalism, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param journalism 实例对象
     * @return 实例对象
     */
    @Override
    public Journalism insert(Journalism journalism) {
//        this.journalismMapper.insert(journalism);
        return journalism;
    }

    /**
     * 修改数据
     *
     * @param journalism 实例对象
     * @return 实例对象
     */
    @Override
    public Journalism update(Journalism journalism) {
//        this.journalismMapper.update(journalism);
        return this.queryById(journalism.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Transactional
    @Override
    public void deleteById(Integer id) {
        journalismMainMapper.deleteById(id);
        journalismMapper.deleteById(id);
    }

    /**
     * 新增或修改数据
     * @param journalismDTO
     */
    @Transactional
    @Override
    public void saveOrUpdate(JournalismDTO journalismDTO) {
        if(journalismDTO.getId() == null){
            // 新增信息
            journalismMapper.insert(journalismDTO);
            // 要返回新增数据的id值来作为主要内容的journalismId
            Integer journalismId = journalismDTO.getId();
            JournalismMain journalismMain = new JournalismMain();
            journalismMain.setMainBody(journalismDTO.getMainBody());
            journalismMainMapper.insert(journalismMain,journalismId);
        }else {
            // 修改信息
            journalismMapper.update(journalismDTO);
            JournalismMain journalismMain = new JournalismMain();
            journalismMain.setMainBody(journalismDTO.getMainBody());
            journalismMain.setJournalismId(journalismDTO.getId());
            journalismMainMapper.update(journalismMain);
        }




    }

    @Override
    public List<Journalism> queryJournalismByPage() {
        List<Journalism> journalismList = journalismMapper.getAllJournalism();

        return  journalismList;
    }

    @Override
    public JournalismDetailVO queryDetailJournalismById(Integer id) {
        Journalism journalism = queryById(id);
        JournalismDetailVO journalismDetailVO = journalismMainMapper.getByjournalismId(id);
        journalismDetailVO.setTitle(journalism.getTitle());
        journalismDetailVO.setTime(journalism.getTime());
        journalismDetailVO.setLabel(journalism.getLabel());
        return journalismDetailVO;
    }
}
